package com.crm.dao.before_sale;

import com.crm.pojo.PlanDetail;
import com.crm.pojo.SalePlan;
import com.crm.vo.SalePlanDetailsVO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by dr5hx on 2018/8/9
 */
public interface SalePlanDAO extends CrudRepository<SalePlan, Long>, JpaSpecificationExecutor<SalePlan> {
    //删除一个无用的方法

    /**
     * 显示对应的客户的计划
     *
     * @param cid
     * @return
     */
    @Query("from SalePlan  saleplan where saleplan.goalClientId=?1 and saleplan.salePlanIsDel='否'")
    public List<SalePlan> showCustomerPlan(BigDecimal cid);

    /**
     * 根据id删除一个销售计划
     * @param planId
     */
    @Query(nativeQuery = true,value = "update SALE_PLAN set SALE_PLAN_IS_DEL='是' where PLAN_ID=?1")
    @Modifying
    public void deleteSalePlanById(Long planId);

    /**
     * 显示销售计划对应的计划明细列表
     * @param salePlan
     * @return
     */
    @Query("select new com.crm.vo.SalePlanDetailsVO(pl,pr,cl.cname,us.uname) from PlanDetail pl,ProductionDetail pr,ClientInfo cl,Userinfo us" +
            " where cl.cid=pl.clientId and us.uId=pl.userId and pr.pid=pl.salePlanId and pl.salePlan=?1 and pl.planDetailIsDel='否'")
    public List<SalePlanDetailsVO> getSalePlanDetatils(SalePlan salePlan);


    /**
     * 根据id获取一条计划明细
     * @param saleDetId
     * @return
     */
    @Query("select new com.crm.vo.SalePlanDetailsVO(pl,pr,cl.cname,us.uname) from PlanDetail pl,ProductionDetail pr,ClientInfo cl,Userinfo us" +
            " where cl.cid=pl.clientId and us.uId=pl.userId and pr.pid=pl.salePlanId  and pl.planDetailIsDel='否' and pl.saleDetId=?1")
    public SalePlanDetailsVO getOnePlanDetails(Long saleDetId);






}
